package com.thx.appserver.action;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.appserver.model.CappSoft;
import com.thx.appserver.service.CappSoftManager;
import com.thx.appserver.util.AnalysisApk;
import com.thx.common.struts2.CurdAction;
import com.thx.common.util.AppContext;
import com.thx.common.web.ActionResult;
import com.thx.file.service.FileManager;
import com.thx.file.util.WebFileUtil;


/**
 * 建立日期 : Jul 31, 2012 4:25:39 PM<br>
 * 作者 : 乔红红<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Namespace("/appserver")
@Action(value="cappsoft", results = {
		@Result(name = "input",  location = "/com/thx/appserver/cappsoft/input.jsp"),
		@Result(name = "list",  location = "/com/thx/appserver/cappsoft/list.jsp"),
		@Result(name = "view",  location = "/com/thx/appserver/cappsoft/detail.jsp"),
		@Result(name = "json", type = "json", params = { "root", "result" }), })
@Controller
@Scope("prototype")
public class CappSoftAction extends CurdAction<CappSoft> {

	private static final long serialVersionUID = 1L;


	@Autowired
	private CappSoftManager cappSoftManager;
	@Autowired
	 FileManager fileManager;
	private String uploadId;
	private String appJson;

	public CappSoftAction() {
	}

	protected void doList() throws Exception {
		list =cappSoftManager.searchCappSoft();
	}



	protected void doView() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			entity =cappSoftManager.get(Integer.valueOf(id));
			if (entity == null) {
				setResult(ActionResult.FAIL, "要查看的记录不存在！！！");
			}
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	protected void doDelete() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			cappSoftManager.delete(Integer.valueOf(id));
			setResult(ActionResult.SUCCESS, "删除成功！！！");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	protected void doDelMulti() throws Exception {
		if (delId != null && delId.length > 0) {
			Integer[] delIds=new Integer[delId.length];
			for(int i=0;i<delId.length;i++){
				delIds[i]=Integer.valueOf(delId[i]);
			}
			cappSoftManager.delMulti(delIds);
			setResult(ActionResult.SUCCESS, "删除成功！！！");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	protected void doInput() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			entity = cappSoftManager.get(Integer.valueOf(id));
			if (entity == null) {
				setResult(ActionResult.FAIL, "要修改的记录不存在！！！");
			}
		}
	}
	protected void doSave() throws Exception {
		try {
			System.out.println("id:"+id);
			String downPathString=null;
		     if(StringUtils.isNotBlank(entity.getVerPath())){
					 downPathString=AppContext.getProperties("apk_down_path")+entity.getVerPath();
		     }
			if (StringUtils.isBlank(id) || id.equals("0") ) {
				entity.setVerPath(downPathString);
				cappSoftManager.save(entity);
				setResult(ActionResult.SUCCESS, "添加成功！");
			} else {


				entity.setId(Integer.valueOf(id));
				cappSoftManager.save(entity);
				setResult(ActionResult.SUCCESS, "修改成功！");
			}

		} catch (Exception e) {
			if (StringUtils.isBlank(id) || id.equals("0")) {
				setResult(ActionResult.FAIL, "添加失败！");
			} else {
				setResult(ActionResult.FAIL, "修改失败！");
			}
			logger.error(e.getMessage(), e);
		}
	}
	public String scanApk() {
		CappSoft baseAppApk = new CappSoft();
		try {
			if (StringUtils.isNotBlank(uploadId)) {
				com.thx.file.model.File file = WebFileUtil
						.getFileById(uploadId);
				if (file != null) {

						Map<String, String> maps = AnalysisApk.unZip(
								file.getPath(), 0);
						CappSoft baApk = cappSoftManager.getNewCappSoft();
						if (baApk != null) {
							if (Integer.valueOf(maps.get("versionCode")) <= baApk
									.getVerCode()) {
								setResult(ActionResult.FAIL,
										"当前Apk版本号不大于数据库中版本，请重新上传");
								return "json";
							}

						}
						baseAppApk.setAppName(file.getOldName());
						baseAppApk.setVerCode(Integer.valueOf(maps
								.get("versionCode")));

						baseAppApk.setVerName(maps.get("versionName"));
				}
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		appJson = com.alibaba.fastjson.JSON.toJSONString(baseAppApk);
		setResult(ActionResult.SUCCESS, appJson);
		return "json";

	}
	public CappSoftManager getCappSoftManager() {
		return cappSoftManager;
	}

	public void setCappSoftManager(CappSoftManager cappSoftManager) {
		this.cappSoftManager = cappSoftManager;
	}


	public FileManager getFileManager() {
		return fileManager;
	}

	public void setFileManager(FileManager fileManager) {
		this.fileManager = fileManager;
	}

	public String getUploadId() {
		return uploadId;
	}

	public void setUploadId(String uploadId) {
		this.uploadId = uploadId;
	}

	public String getAppJson() {
		return appJson;
	}

	public void setAppJson(String appJson) {
		this.appJson = appJson;
	}


}
